package com.mobilnaElka.models.search;

import java.io.File;
import java.util.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

import android.util.Log;

import com.mobilnaElka.AppConsts;
import com.mobilnaElka.activities.search.SearchTabActivity;

public class XMLReader {
	File fXmlFile;
	ArrayList<Map<String, String>> stuff;

	public XMLReader(String fileName, SearchTabActivity activity) {
		fXmlFile = new File(fileName);
		stuff = new ArrayList<Map<String, String>>();
	}

	public void readXML() {

		if (!fXmlFile.exists())
		{
			Log.e(AppConsts.TAGNAME, "Plik xml nie istnieje");
			return;
		}
		
		try {

			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("collection");

			for (int coll = 0; coll < nList.getLength(); coll++) {
				NodeList nnList = doc.getElementsByTagName("ns2:author");
				for (int temp = 0; temp < nnList.getLength(); temp++) {

					Node nNode = nnList.item(temp);

					if (nNode.getNodeType() == Node.ELEMENT_NODE) {

						Element eElement = (Element) nNode;

						Log.i(AppConsts.TAGNAME,
								Consts.XML_NAME
										+ " : "
										+ getTagValue(Consts.XML_NAME, eElement));

						Log.i(AppConsts.TAGNAME,
								Consts.XML_SURNAME
										+ " : "
										+ getTagValue(Consts.XML_SURNAME,
												eElement));

						Map<String, String> sm = new TreeMap<String, String>();
						putValues(eElement, sm);

						eElement = getElement("ns2:authorprofile", eElement);
						putValues(eElement, sm);

						eElement = getElement("ns2:file", eElement);
						putValues(eElement, sm);

						stuff.add(sm);

					}
				}
			}

		} catch (Exception e) {
			Log.e(AppConsts.TAGNAME, e.getMessage(), e);
		}
	}

	private void putValues(Element eElement, Map<String, String> sm) {
		String[] options = Consts.XML_OPTIONS;
		for (int i = 0; i < options.length; i++) {
			String option = options[i];
			if (!sm.containsKey(option) || option.equals(Consts.XML_ID))
				sm.put(option, getTagValue(option, eElement));
		}

	}

	private Element getElement(String sTag, Element eElement) {
		Element item = (Element) eElement.getElementsByTagName(sTag).item(0);
		return item;

	}

	private static String getTagValue(String sTag, Element eElement) {

		if (eElement==null)
			return "";
		Node item = eElement.getElementsByTagName(sTag).item(0);

		if (item == null)
			return "";

		NodeList nlList = item.getChildNodes();
		Node nValue = nlList.item(0);

		if (nValue == null)
			return "";

		return nValue.getNodeValue();

	}

	public Map<String, String> getStuffMember(int i) {
		if (stuff.size()>0)
			return stuff.get(i);
		else return null;
	}

	public int getSize() {
		return stuff.size();
	}


}